Walkthrough 11-6: Coerce and format strings, numbers, and dates
In this walkthrough, you continue to work with the XML flights data posted to the postMultipleFlights flow. You will:
· Coerce data types.
· Format strings, numbers, and dates.
Starting file
If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.
Format a string
1. Return to the Transform Message properties view for the transformation in postMultipleFlights.
2. Use the upper function to return the plane value in uppercase.
3. Coerce the argument to a String.
plane: upper(object.planeType as String)
4. Look at the preview.
Coerce a string to a number
5. In the preview, look at the data type of the prices; you should see that they are strings.
6. Change the DataWeave expression to use the as keyword to return the prices as numbers.
price: object.price as Number,
7. Look at the preview.
8. Change the output type from application/dw to application/java.
9. Look at the preview; you should see the prices are now either Integer or Double objects.
Coerce a string to a specific type of number object
10. Change the DataWeave expression to use the class metadata key to coerce the prices to java.lang.Double objects.
price: object.price as Number {class:"java.lang.Double"},
11. Look at the preview; you should see the prices are now all Double objects.
12. Change the output type from application/java back to application/dw.
Format a number
13. Remove the coercion of the price to a java.lang.Double.
14. Coerce the price to a String and use the format schema property to format the prices to zero decimal places.
price: object.price as Number as String {format: "###"},
15. Look at the preview; the prices should now be formatted.
16. Change the DataWeave expression to format the prices to two decimal places.
price: object.price as Number as String {format: "###.##"},
17. Look at the preview.
18. Change the DataWeave expression to format the prices to two minimal decimal places.
price: object.price as Number as String {format: "###.00"},
19. Look at the preview; all the prices should now be formatted to two decimal places.
Note: If you are not a Java programmer, you may want to look at the Java documentation for the DecimalFormatter class to review documentation on DecimalFormat patterns. https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html
Coerce a string to a date
20. Add a date field to the return object.
date: object.departureDate
21. Look at the preview; you should see the date property is a String.
22. Change the DataWeave expression to use the as operator to convert departureDate to a date object; you should get issues displayed.
date: object.departureDate as Date
23. Look at the errors.
24. Look at the format of the dates in the preview.
25. Change the DataWeave expression to use the format schema property to specify the pattern of the input date strings.
date: object.departureDate as Date {format: "yyyy/MM/dd"}
Note: If you are not a Java programmer, you may want to look at the Java documentation for the DateTimeFormatter class to review documentation on pattern letters. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
26. Look at the preview; you should see date is now a Date object.
Format a date
27. Use the as operator to convert the dates to strings, which can then be formatted.
date: object.departureDate as Date {format: "yyyy/MM/dd"} as String
28. Look at the preview section; the date is again a String – but with a different format.
29. Use the format schema property with any pattern letters and characters to format the date strings.
date: object.departureDate as Date {format: "yyyy/MM/dd"} as String {format: "MMM dd, yyyy"}
30. Look at the preview; the dates should now be formatted according to the pattern.